import { computed, ref } from 'vue';
import { useDigital } from '../../../utils/useDigital';

const { formatPrice } = useDigital();

export const useComputed = (props, desc) => {
  /**
   * @description 详情标题左侧竖线
   * @param {item} rowItem
   * @return {Boolean} prefix
   */
  const showPrefix = computed(() => {
    return item => {
      return item.prefix && props.prefix;
    };
  });

  /**
   * @description 计算行间距
   * @param {item} rowItem
   * @return {Boolean} gutter
   */
  const getGutter = computed(() => {
    return item => {
      return item.gutter ? item.gutter : props.gutter;
    };
  });

  /**
   * @description 获取含有插槽属性的数组集合
   * @param {list} itemList
   * @return {Array} slotsArray
   */
  const getSlots = computed(() => {
    return list => {
      let slotNames = ref([]);
      list &&
        list.map(item => {
          if (item.itemSlot) slotNames.value.push({ slot: item.itemSlot, ...item });
          if (item.labelSlot) slotNames.value.push({ slot: item.labelSlot, ...item });
          if (item.valueSlot) slotNames.value.push({ slot: item.valueSlot, ...item });
        });
      return slotNames.value;
    };
  });

  /**
   * @description 获取不隐藏的列数组
   * @return {Array} colList
   */
  const getColList = computed(() => {
    return props.itemList.filter(({ hidden }) => !hidden);
  });

  /**
   * @description 获取每个栅格的数据
   * @param {item} item
   * @return {String} colValue
   */
  const getColValue = computed(() => {
    return item => {
      let { value, dictCode, type } = item;
      value = dictCode ? desc.data[dictCode] : value;
      if (type === 'amount') return formatPrice(value);
      return value;
    };
  });

  /**
   * @description 标签是否存在分号
   * @param {item} item
   * @return {String} ':' or ''
   */
  const hasColon = computed(() => {
    return item => {
      return item.colon || desc.colon ? ':' : '';
    };
  });

  /**
   * @description 计算行间距
   * @return {Object} { gutter: x }
   */
  const rowAttrs = computed(() => {
    return { gutter: props.gutter };
  });

  /**
   * @description 计算列宽
   * @return {Object} { span: x }
   */
  const colAttrs = computed(() => {
    return { span: props.item.col || 6 };
  });

  return {
    showPrefix,
    getGutter,
    getSlots,
    getColList,
    getColValue,
    hasColon,
    rowAttrs,
    colAttrs,
  };
};
